﻿Imports Microsoft.Office.Interop

Public Class Form2

  Public alertInterval As String
  Public audio As Boolean
  Public visual As Boolean

  Public oApp As Outlook.Application = New Outlook.Application  'outlook app
  Public oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")   'namespace object
  Public oAccounts As Outlook.MAPIFolder() = New Outlook.MAPIFolder(oNS.Folders.Count) {}  'array of accounts
  
  Public checks() As Windows.Forms.CheckedListBox = New CheckedListBox(oNS.Folders.Count) {} 'array of checked listboxes for holding the actual objects

  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    alertInterval = TextBox1.Text
    audio = True
    visual = True

    For i As Integer = 1 To (oNS.Folders.Count)
      oAccounts(i - 1) = oNS.Folders(i)
    Next

    For i As Integer = 0 To (oAccounts.Count - 2)
      mailAccounts.Items.Insert(i, oAccounts(i).Name)
    Next

    For i As Integer = 0 To (oAccounts.Count - 2)
      checks(i) = New CheckedListBox
      For j As Integer = 0 To (oAccounts(i).Folders.Count - 1)
        checks(i).Items.Add(oAccounts(i).Folders(j + 1))
      Next
    Next
  End Sub

  Private Sub ApplyBtn_Click(sender As Object, e As EventArgs) Handles ApplyBtn.Click
    alertInterval = TextBox1.Text ' Retrieves time interval entered in seconds

    ' Check if audio alert is enabled
    If AudioCheck.Checked Then
      audio = True
    Else : audio = False
    End If

    ' Check if visual alert is enabled
    If VisualCheck.Checked Then
      visual = True
    Else : visual = False
    End If

    For i As Integer = 0 To (oAccounts(mailAccounts.SelectedIndex).Folders.Count - 1)
      If mailFolders.GetItemCheckState(i) Then
        checks(mailAccounts.SelectedIndex).SetItemChecked(i, True)
      Else : checks(mailAccounts.SelectedIndex).SetItemChecked(i, False)
      End If
    Next

  End Sub

  Private Sub CloseBtn_Click(sender As Object, e As EventArgs) Handles CloseBtn.Click
    Me.Close()
  End Sub

  Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  End Sub

  Private Sub mailAccounts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles mailAccounts.SelectedIndexChanged
    mailFolders.Items.Clear()
    For i As Integer = 0 To oAccounts(mailAccounts.SelectedIndex).Folders.Count - 1
      mailFolders.Items.Insert(i, oAccounts(mailAccounts.SelectedIndex).Folders(i + 1).Name)
      If checks(mailAccounts.SelectedIndex).GetItemCheckState(i) Then mailFolders.SetItemChecked(i, True)
    Next
    mailFolders.Focus()
  End Sub


End Class

